package com.ruoyi.controller;

import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.handler.timeout.TimeoutException;
import com.ruoyi.pojo.Balance;
import com.ruoyi.pojo.Goods;
import com.ruoyi.pojo.Order;
import com.ruoyi.service.BalanceService;
import com.ruoyi.service.DubboDemoService;
import com.ruoyi.service.GoodsService;
import com.ruoyi.serviceimpl.OrderServiceImpl;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;



@RefreshScope//nacos配置文件动态刷新
@RestController
@RequestMapping("/provider")
public class ProviderController {
    @Value("${server.port}")
    private String post;
    @Value("${spring.application.name}")
    private String name;

    /**
     * 用户余额接口服务
     */
    @DubboReference()
    private BalanceService balanceService;

    /**
     * 商品接口服务
     */
    @DubboReference()
    private GoodsService goodsServiceImpl;


    @Resource
    private OrderServiceImpl orderService;

    @GetMapping("/getpost")
    public String getPost(){
        return "当前端口："+post;
    }

    @GetMapping("/getname")
    public String getName(){
        return "当前服务名："+name;
    }


    @GetMapping("/getdubbo")
    private Map<String,Object> getDubbo(){
        Object balanceSay = balanceService.sayHello("dubbo");
        List<Balance> balanceList = null;
        try {
            balanceList = balanceService.getBalanceList();
        } catch (Exception e) {

            throw new RuntimeException("调用balance服务失败"+e);
        }

        String goodsSay = goodsServiceImpl.sayHello("dubbo");
        List<Goods> goodsList = goodsServiceImpl.getGoodsList();
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("balanceSay",balanceSay);
        map.put("balance",balanceList);
        map.put("goodsSay",goodsSay);
        map.put("goodsList",goodsList);
        return map;
    }


    /**
     * 创建订单
     * @return
     */
    @GetMapping("/insertOrder")
    public String insertOrder(){
        orderService.inserOrder(new Order(1, "1",new BigDecimal(10),1));
        return "创建订单成功";
    }



}
